package bhbt.controller.admin.wealth;


import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bhbt.controller.core.BaseController;
import bhbt.model.Admin;
import bhbt.model.Daili;
import bhbt.model.Member;
import bhbt.model.SysPay;
import bhbt.service.DailiService;
import bhbt.service.MemberService;
import bhbt.service.SysPayService;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;





/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
 
public class SysPayAddController extends BaseController {
    @Resource
    private SysPayService sysPayService;
    @Resource
    private MemberService memberService;
    @Resource
    private DailiService dailiService;
    
    @Resource(name = "taskExecutor")
    private TaskExecutor taskExecutor;
   
    @RequestMapping("admin/wealth/sysPayAdd.shtml")
    public String add(SysPay sysPay,ModelMap model,HttpServletRequest request, HttpServletResponse response) throws Exception {
    	Map formMap = getParameterMap(request);
    	//新增
    	Admin admin = ContextUtil.getAdmin();
    	String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
    	 
    	String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
    	 
    	//判断安全密码
    	if(!admin.getTwoPassword().equals(pwd)){
    		setMessage(model,"管理员安全密码不正确。");
    		return "admin/wealth/sysPayAdd";
    		
    	}
    	
    	String loginName = MapUtil.getString(formMap, "loginName");
    	Member member = memberService.executMemberByLoginName(loginName);
    	if(member==null){
    		setMessage(model,"会员编号不存在。");
    		return "admin/wealth/sysPayAdd";
    	}
    	
    	 
		sysPayService.save(sysPay,member);
    	
		return "redirect:sysPayList.shtml";
    	
    	
		
    }
    
    /**
     * loginName检测  ajax
     * @param request
     * @return
     * @throws Exception
     */
	@RequestMapping("admin/wealth/ajaxGetMemberByName.shtml")
	@ResponseBody
	public void ajaxGetMemberByName( HttpServletRequest request, HttpServletResponse response) {
		
		
		Map formMap = getParameterMap(request); 
		String loginName= MapUtil.getString(formMap,"loginName");
		Member member=memberService.executMemberByLoginName(loginName);
		 
		HashMap resultformMap = new HashMap();
		if(member==null){
			
			renderSuccess(response, false, resultformMap);
		}else{
			 
			resultformMap.put("personName", member.getPersonName());
			 
			renderSuccess(response, true, resultformMap); 
			
		}
		
	}
  
}
